﻿@{
    Layout = "~/Shared/Layouts/_AdminLayout.cshtml";
    Page.Title = "Категории";
    
    var categoryTitle = "";
    
    var db = Database.Open("VirtualeShop");
    
    if(IsPost && !Request["categoryTitle"].IsEmpty())
    {
        categoryTitle = Request["categoryTitle"];
        if(!Admin.CategoryExists(categoryTitle))
        {
            var sqlInsert = "INSERT INTO Categories (CategoryTitle) VALUES (@0)";
            db.Execute(sqlInsert, categoryTitle);
        }
        else
        {
            ModelState.AddError("categoryTitle", "Категоријата веќе постои");
        }
    }
    
    var sqlCommand = "SELECT * FROM Categories";
    var categories = db.Query(sqlCommand);
    
}

<h1>Категории</h1>

<table class="grid">
    <tr>
        <th colspan="3" class="gridHeader">Категорија</th>
    </tr>
    @foreach (var category in categories)
    {
        <tr>
            <td id="mainColumn">
                @category.categoryTitle
            </td>
            <td>
                <a href="EditCategory/@category.categoryId">Измени</a>
            </td>
            <td>
                <a href="DeleteCategory/@category.categoryId">Избриши</a>
            </td>
        </tr> 
    }
</table>

<h2>Додади нова категорија</h2>
<form action="" method="post">
    <p>
        Наслов на категорија:
        @Html.TextBox("categoryTitle", categoryTitle)
        <input type="submit" value="Додади" />
        @Html.ValidationMessage("categoryTitle")
    </p>
</form>